let lyricHeight = 0;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isLyricShow: Boolean,
    lyric: String
  },


  observers: {
    lyric(lrc) {
      if (lrc == "暂无歌词" || lrc == "") {
        this.setData({
          lrcList: [{
            lrc: "暂无歌词",
            time: 0
          }],
          nowLyricIndex: -1
        })
      } else {
        this._parseLyric(lrc);
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    lrcList: [],
    nowLyricIndex: 0,
    scrollTop: 0
  },

  lifetimes: {
    ready() {
      wx.getSystemInfo({
        success(res) {
          lyricHeight = parseInt(res.screenHeight) / 750 * 64;
        },
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    update(currentTime) {
      const lrcList = this.data.lrcList;

      if (lrcList[lrcList.length - 1].time == 0) {
        return;
      }

      if (currentTime > lrcList[lrcList.length - 1].time) {
        if (this.data.nowLyricIndex != -1) {
          this.setData({
            nowLyricIndex: -1,
            scrollTop: lrcList.length * lyricHeight
          });
        }
      }

      for (let i = 0; i < lrcList.length; i++) {
        if (currentTime <= lrcList[i].time) {
          this.setData({
            nowLyricIndex: i - 1,
            scrollTop: (i - 1) * lyricHeight
          });
          break;
        }
      }
    },

    // 歌词转换
    _parseLyric(slyric) {
      let line = slyric.split("\n");
      let _lrcList = [];
      line.forEach((elem) => {
        let time = elem.match(/\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]/g);
        if (time != null) {
          let lrc = elem.split(time)[1];
          let timeReg = time[0].match(/(\d{2,}):(\d{2})(?:\.(\d{2,3}))/);
          // console.log(timeReg);
          let time2Sec = parseInt(timeReg[1]) * 60 + parseInt(timeReg[2]) + parseInt(timeReg[3]) / 1000;
          _lrcList.push({
            lrc,
            time: time2Sec
          });
        }
      });
      this.setData({
        lrcList: _lrcList
      })
    },
  }
})